﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.UI.Properties;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.LogicalInferenceMechanism
{
    /// <summary>
    /// Класс формы с результатами привязки ситуаций
    /// </summary>
    public partial class SituationConsiderationResultsForm : RestorableForm
    {
        private ProjectDescriptor _curProject; //текущий проект
        private Guid _examinedSituationId; //идентификатор рассмотренной ситуации

        /// <summary>
        /// Возвращает рассмотренную ситуацию
        /// </summary>
        private Situation _examinedSituation
        {
            get
            {
                return _curProject.GetItemById(_examinedSituationId);
            }
        }

        /// <summary>
        /// Заполняет результаты
        /// </summary>
        private void FillResults()
        {
            Situation sit = _examinedSituation;
            Text = Text + "\"" + sit.Name + "\"";
            bool resulttrue = (bool)sit.InferenceResult;
            if (resulttrue)
            {
                ResultLabel.Text = Resources.BI_SITUATIONSHIERARCHY_LIM_EXAMINATION_RESULT_OK;
                ResultLabel.ForeColor = Color.Green;
            }
            int c = sit.ItemsCount;
            DataGridViewRowCollection rows = CharacteristicsExaminationDataGrid.Rows;
            for (int i = 0; i < c; i++)
            {
                Characteristic ch = sit.GetItemByPosition(i);
                IndicatorType it = ch.IndicatorType;
                if ((!Indicator.IsSituationType(it)) && (!Indicator.IsInternalType(it)))
                {
                    if (it != IndicatorType.RulesContainer)
                    {
                        rows.Add();
                        object expval = ch.ExpectedValue;
                        object realval = ch.RealValue;
                        bool identical = true;                        
                        if ((!GeneralMethods.Identical(expval, realval)) && (expval != null))                        
                        {
                            if ((ch.IndicatorType == IndicatorType.DateAndFactCorrelations) && (!GeneralMethods.IsNull(realval)) && (!GeneralMethods.IsNull(expval)))                            
                            {
                                string realstring = realval as string;
                                string realcharacteristicname = realstring.Substring(0, realstring.IndexOf("|", StringComparison.InvariantCulture));
                                string expcharacteristicname = expval as string;
                                identical = realcharacteristicname == expcharacteristicname;
                            }
                            else
                            {
                                identical = false;
                            }
                        }
                        DataGridViewCellCollection cells = rows[rows.Count - 1].Cells;
                        DataGridViewCell cell = cells[0];
                        cell.Value = ch.Name;
                        cell.Style.ForeColor = identical ? Color.Green : Color.Red;
                        string s = Constants.NullDoesNotMatterString;
                        if (expval != null)
                        {
                            s = expval.ToString();
                        }
                        cell = cells[1];
                        cell.Value = s;
                        cell.Style.ForeColor = identical ? Color.Green : Color.Red;
                        s = Constants.NullDoesNotMatterString;
                        if (realval != null)
                        {
                            s = realval.ToString();
                        }
                        cell = cells[2];
                        cell.Value = s;
                        cell.Style.ForeColor = identical ? Color.Green : Color.Red;
                    }
                    else
                    {
                        if (resulttrue)
                        {
                            RulesContainerCombo.Items.Add(ch.Name);
                            RulesContainerCombo.SelectedIndex = RulesContainerCombo.Items.Count - 1;
                            ViewRulesContainer.Enabled = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Отображает диалог просмотра результатов контейнера правил
        /// </summary>
        private void ViewRulesContainerResults()
        {
            Characteristic ch = _examinedSituation.GetFirstItemByName(RulesContainerCombo.SelectedItem.ToString());
            RulesContainerExecutingLogDialog pdl = new RulesContainerExecutingLogDialog(_curProject, ch);
            pdl.ShowDialog();
        }

        /// <summary>
        /// Инициализация компонентов
        /// </summary>
        public SituationConsiderationResultsForm(ProjectDescriptor currentProject, Guid examinedSituationId)
        {
            _curProject = currentProject;
            _examinedSituationId = examinedSituationId;
            InitializeComponent();
        }

        private void SituationExaminationResultsDialog_Load(object sender, EventArgs e)
        {
            FillResults();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ViewRulesContainer_Click(object sender, EventArgs e)
        {
            ViewRulesContainerResults();
        }

        private void AbortDebug_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
